package com.ruoyi.employee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.dto.TbCheDto;
import com.ruoyi.dto.TbEmployeeDto;
import com.ruoyi.employee.mapper.*;
import com.ruoyi.employee.service.*;
import com.ruoyi.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author c'c'l
* @description 针对表【tb_employee(员工信息表)】的数据库操作Service实现
* @createDate 2024-08-29 10:02:09
*/
@Service
public class TbEmployeeServiceImpl extends ServiceImpl<TbEmployeeMapper, TbEmployee>
    implements TbEmployeeService{
    @Autowired
    private TbEmployeeMapper tbEmployeeMapper;
    @Autowired
    private TbEmployeeRoleMapper tbEmployeeRoleMapper;
    @Autowired
    private TbBuildingblockEmployeeMapper tbBuildingblockEmployeeMapper;
    @Autowired
    private TbEmployeeRoleService tbEmployeeRoleService;
    @Autowired
    private TbEmployeeBuildingblockService tbEmployeeBuildingblockService;
    @Autowired
    private TbCheEmployeeService tbCheEmployeeService;
    @Autowired
    private TbCardMapper tbCardMapper;
    @Autowired
    private TbEmployeeCardMapper tbEmployeeCardMapper;
//    @Autowired
//    private TbEmployeeService tbEmployeeService;
    @Autowired
    private TbEmployeeBuildingblockMapper tbEmployeeBuildingblockMapper;
    @Autowired
    private TbCheEmployeeMapper tbCheEmployeeMapper;


    @Override
    public IPage<TbEmployeeDto> pageEmployee(Page<TbEmployeeDto> page, TbEmployeeDto tbEmployeeDto) {
        List<Long> emplIdList = tbEmployeeRoleMapper.findEmployeeIds(tbEmployeeDto.getRoleId());
        tbEmployeeDto.setEmplIdList(emplIdList);

        if(emplIdList.size() == 0 && tbEmployeeDto.getRoleId() != null) {
            return new Page<>(1,2);
        }
        IPage<TbEmployeeDto> iPage = tbEmployeeMapper.pageEmployee(page, tbEmployeeDto);
        List<TbEmployeeDto> employeeList = iPage.getRecords();

        Map<String,List<String>> roleNameMap = new HashMap<>();
        Map<String,List<String>> budingNameMap = new HashMap<>();
//        List<String> roleNameList = new ArrayList<>();
        employeeList.stream().forEach(item -> {
            List<String> roleNameList = tbEmployeeRoleMapper.findEmplRoleName(item.getId());
            List<String> budingNameList = tbBuildingblockEmployeeMapper.findEmpManageBudingName(item.getId());
            roleNameMap.put(item.getId().toString(),roleNameList);
            budingNameMap.put(item.getId().toString(),budingNameList);
        });
        iPage.getRecords().stream().forEach(empl -> {
            List<String> list = roleNameMap.get(empl.getId().toString());
            List<String> strings = budingNameMap.get(empl.getId().toString());
            empl.setBudingNameList(strings);
            empl.setRoleNameList(list);
        });


        return iPage;
//        for (TbEmployeeDto tbEmployee : employeeList) {
//            Long id = tbEmployee.getId();
//            roleNameList = tbEmployeeRoleMapper.findEmplRoleName(id);
//            listMap.put(id.toString(), roleNameList);
//        }
    }

    @Override
    @Transactional
    public void addEmployee(TbEmployeeDto tbEmployeeDto) {
        TbEmployee tbEmployee = new TbEmployee();
        BeanUtils.copyProperties(tbEmployeeDto,tbEmployee);
        tbEmployee.setPassword("123456");
        tbEmployee.setFaceImg("https://img-s-msn-com.akamaized.net/tenant/amp/entityid/AA1pdsfF.img?w=640&h=427&m=6");
        tbEmployee.setStatus(0);
        tbEmployee.setIsnoRele(0);
        tbEmployee.setCreateTime(new Date());
        tbEmployee.setIsDeleted(0);
        tbEmployeeMapper.insert(tbEmployee);

        String cardNum = tbEmployeeDto.getCardNum();
        String cardtype = tbEmployeeDto.getCardtype();
//        TbCard tbCard = tbCardMapper.selectOne(
//                new LambdaQueryWrapper<TbCard>().eq(TbCard::getCardName, cardtype)
//        );
        TbEmployeeCard tbEmployeeCard = new TbEmployeeCard();
        tbEmployeeCard.setEmployeeId(tbEmployee.getId());
        tbEmployeeCard.setCardId(Long.parseLong(cardtype));
        tbEmployeeCard.setCardNum(cardNum);
        tbEmployeeCardMapper.insert(tbEmployeeCard);


        List<Long> roleIdList = tbEmployeeDto.getAddRoleIdList();
        List<TbEmployeeRole> tbEmployeeRoleList = new ArrayList<>();
        for (Long roleId : roleIdList) {
            TbEmployeeRole tbEmployeeRole = new TbEmployeeRole();
            tbEmployeeRole.setEmployeeId(tbEmployee.getId());
            tbEmployeeRole.setRoleId(roleId);
            tbEmployeeRole.setIsDeleted(0);
            tbEmployeeRole.setCreateTime(new Date());
            tbEmployeeRoleList.add(tbEmployeeRole);
        }
        tbEmployeeRoleService.saveBatch(tbEmployeeRoleList);

        List<String> budingIdList = tbEmployeeDto.getBudingIdList();
        List<TbEmployeeBuildingblock> tbEmployeeBuildingblockList = new ArrayList<>();
        for (String budingId : budingIdList) {
            TbEmployeeBuildingblock tbEmployeeBuildingblock = new TbEmployeeBuildingblock();
            tbEmployeeBuildingblock.setBuildingblockId(Long.parseLong(budingId));
            tbEmployeeBuildingblock.setEmployeeId(tbEmployee.getId());
            tbEmployeeBuildingblock.setIsDeleted(0);
            tbEmployeeBuildingblock.setCreateTime(new Date());
            tbEmployeeBuildingblockList.add(tbEmployeeBuildingblock);
        }
        tbEmployeeBuildingblockService.saveBatch(tbEmployeeBuildingblockList);

        List<TbCheDto> cheList = tbEmployeeDto.getCheList();
        List<TbCheEmployee> cheEmployeeList = new ArrayList<>();
        for (TbCheDto cheDto : cheList) {
            if(!StringUtils.isEmpty(cheDto.getStartBuilding())) {
            TbCheEmployee tbCheEmployee = new TbCheEmployee();
            tbCheEmployee.setEmployeeId(tbEmployee.getId());
            tbCheEmployee.setCheNumber(cheDto.getStartBuilding());
            cheEmployeeList.add(tbCheEmployee);
            }
        }
        tbCheEmployeeService.saveBatch(cheEmployeeList);
    }

    @Override
    public Integer blockupEmployee(TbEmployeeDto tbEmployeeDto) {
        Integer status = 0;
        if(tbEmployeeDto.getStatus() == 0) {
            status = 1;
        }
        TbEmployee tbEmployee = new TbEmployee();
        tbEmployee.setId(tbEmployeeDto.getId());
        tbEmployee.setStatus(status);
        int i = tbEmployeeMapper.updateById(tbEmployee);
        return i;
    }

    @Override
    public TbEmployeeDto getEmployeeById(Long id) {
//        TbEmployee tbEmployee = tbEmployeeService.getById(id);
        TbEmployee tbEmployee = tbEmployeeMapper.selectById(id);
        TbEmployeeDto tbEmployeeDto = new TbEmployeeDto();
        BeanUtils.copyProperties(tbEmployee,tbEmployeeDto);
        TbEmployeeCard tbEmployeeCard = tbEmployeeCardMapper.selectOne(
                new LambdaQueryWrapper<TbEmployeeCard>().eq(TbEmployeeCard::getEmployeeId, id)
        );
        TbCard tbCard = tbCardMapper.selectOne(
                new LambdaQueryWrapper<TbCard>().eq(TbCard::getId, tbEmployeeCard.getCardId())
        );
        tbEmployeeDto.setCardtype(tbCard.getCardName());
        tbEmployeeDto.setCardNum(tbEmployeeCard.getCardNum());
        List<String> employeeRoleIdList =tbEmployeeRoleMapper.findUpdateEmployeeRoleId(id);
        List<String> employeeBuildingIdList =tbEmployeeBuildingblockMapper.findEmployeeBuildingId(id);
        List<TbCheDto> employeeCheIdList = tbCheEmployeeMapper.findEmployeeCheId(id);
        tbEmployeeDto.setRoleIdList(
                employeeRoleIdList.stream()
                        .map(roleId -> Long.parseLong(roleId))
                        .collect(Collectors.toList())
        );
        tbEmployeeDto.setBudingIdList(employeeBuildingIdList);
//        Integer count = 1;
//        for (TbCheDto cheDto : employeeCheIdList) {
//            cheDto.setId(count);
//            count++;
//        }
        tbEmployeeDto.setCheIdList(employeeCheIdList);
        return tbEmployeeDto;
    }

    @Override
    @Transactional
    public void updateEmployee(TbEmployeeDto tbEmployeeDto) {
        //修改员工信息
        TbEmployee tbEmployee = new TbEmployee();
        BeanUtils.copyProperties(tbEmployeeDto,tbEmployee);
        tbEmployee.setUpdateTime(new Date());
        //TODO 图片上传 有问题
        tbEmployeeMapper.updateById(tbEmployee);

        Long employeeid = tbEmployeeDto.getId();

        String cardtype = tbEmployeeDto.getCardtype();
        String cardNum = tbEmployeeDto.getCardNum();

        tbEmployeeCardMapper.delete(
                new LambdaQueryWrapper<TbEmployeeCard>().eq(TbEmployeeCard::getEmployeeId, employeeid)
        );

        TbCard tbCard = tbCardMapper.selectOne(
                new LambdaQueryWrapper<TbCard>().eq(TbCard::getId, cardtype)
        );

        TbEmployeeCard tbEmployeeCard = new TbEmployeeCard();
        tbEmployeeCard.setEmployeeId(employeeid);
        tbEmployeeCard.setCardId(tbCard.getId());
        tbEmployeeCard.setCardNum(cardNum);
        tbEmployeeCardMapper.insert(tbEmployeeCard);

        List<String> budingIdList = tbEmployeeDto.getBudingIdList();
//        if(CollectionUtils.isEmpty(budingIdList)) {
            tbEmployeeBuildingblockMapper.updateIsDeleteById(employeeid);
        for (String budingId : budingIdList) {
//            tbEmployeeBuildingblockMapper.addEmployeeBuilding(employeeid,Long.parseLong(budingId));
            TbEmployeeBuildingblock tbEmployeeBuildingblock = new TbEmployeeBuildingblock();
            tbEmployeeBuildingblock.setEmployeeId(employeeid);
            tbEmployeeBuildingblock.setIsDeleted(0);
            tbEmployeeBuildingblock.setBuildingblockId(Long.parseLong(budingId));
            tbEmployeeBuildingblock.setCreateTime(new Date());
            tbEmployeeBuildingblockMapper.insert(tbEmployeeBuildingblock);
        }
//            budingIdList.forEach(budingId -> {
//                tbEmployeeBuildingblockMapper.addEmployeeBuilding(employeeid,Long.parseLong(budingId));
//            });
//        }

        List<TbCheDto> cheList = tbEmployeeDto.getCheList();
//        if(CollectionUtils.isEmpty(cheList)) {
            tbCheEmployeeMapper.deleteByEmployeeId(employeeid);
        for (TbCheDto cheDto : cheList) {
            TbCheEmployee tbCheEmployee = new TbCheEmployee();
            tbCheEmployee.setEmployeeId(employeeid);
            tbCheEmployee.setCheNumber(cheDto.getStartBuilding());
            tbCheEmployeeMapper.insert(tbCheEmployee);
        }
//            cheList.stream().forEach(cheDto -> {
//                TbCheEmployee tbCheEmployee = new TbCheEmployee();
//                tbCheEmployee.setEmployeeId(employeeid);
//                tbCheEmployee.setCheNumber(cheDto.getStartBuilding());
//                tbCheEmployeeMapper.insert(tbCheEmployee);
//            });
//        }

        List<Long> roleIdList = tbEmployeeDto.getRoleIdList();
//        if(CollectionUtils.isEmpty(roleIdList)) {
            tbEmployeeRoleMapper.updateIsDeleteById(employeeid);
            for (Long roleId : roleIdList) {
                TbEmployeeRole tbEmployeeRole = new TbEmployeeRole();
                tbEmployeeRole.setCreateTime(new Date());
                tbEmployeeRole.setEmployeeId(employeeid);
                tbEmployeeRole.setIsDeleted(0);
                tbEmployeeRole.setRoleId(roleId);
                tbEmployeeRoleMapper.insert(tbEmployeeRole);
            }
//        }
    }

    @Override
    @Transactional
    public Integer deletedEmployeeAndOther(Long employeeId) {
        tbEmployeeMapper.updateIsDeletedByEmployeeId(employeeId);
        tbEmployeeRoleMapper.updateIsDeleteById(employeeId);
        tbEmployeeBuildingblockMapper.updateIsDeleteById(employeeId);
        return 1;
    }
}




